package com.yanqu.backendweb.exception;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.backendweb.utils.eErrorCode;
import lombok.Getter;

/**
 * @author duli
 * @version 1.0
 * @date 2020/9/15 11:46
 * @Description
 */
@Getter
public class BusinessException extends RuntimeException{

    protected int code;

    protected String msg = "";

    public BusinessException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BusinessException(int code, String msg, Throwable throwable) {
        super(msg, throwable);
        this.code = code;
        this.msg = msg;
    }

    public BusinessException(eErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public BusinessException(eErrorCode errorCode, Object... args) {
        this.code = errorCode.getCode();
        this.msg = String.format(errorCode.getMsg(), args);
    }

    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", this.getCode());
        jsonObject.put("msg", this.getMsg());
        return jsonObject;
    }
}
